<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\FollowRequest;
use App\Http\Requests\FollowV2Request;
use App\model\Coustomer;
use App\model\CustomerFollow;
use App\model\CustomerZhongjianSign;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FollowController extends Controller
{
    //
    public function create(FollowRequest $request){
           $follow= new CustomerFollow();
           $follow->customer_id=$request->get('customer_id');
           $follow->desc=$request->get('desc');
           $follow->image=$request->get('image');
           
           $follow->save();
           return response()->json(
               [
                   'status'=>0,
                   'message'=>'保存数据成功',
               ]
           );
    }

    /**
     * 提交跟进v2
     * @bodyParam customer_id int required  客户id. Example:null
     * @bodyParam desc string required  描述. Example:描述
     * @bodyParam image string required  图片. Example:null
     * @bodyParam follow_status string   状态. Example:null
     * @bodyParam time string   状态. Example:null
     * @bodyParam sign  string 客户类型  Example:3,2
     */
    public function createV2(FollowRequest $request){
        $follow= new CustomerFollow();
        $follow->customer_id=$request->get('customer_id');
        $follow->desc=$request->get('desc');
        $follow->image=$request->get('image');
        $follow->time=$request->get('time');
        $follow->user_id=\Auth::guard('api')->id();
        $follow->save();
        $customer=Coustomer::find($request->get('customer_id'));
        $customer->follow_status=$request->get('follow_status');
        if ($request->get('follow_status')==Coustomer::Completed){
            $customer->status=$request->get('follow_status');
        }else{
            $customer->status=Coustomer::Follow_up;
        }
        if ($request->get('sign')){
          $sign=  explode(',',$request->get('sign'));
          if (!empty($sign)){
              foreach ($sign as $value){
                 $customerZhongjianSign= new CustomerZhongjianSign();
                 $customerZhongjianSign->customer_id=$customer->customer_id;
                 $customerZhongjianSign->sign_id=$value;
                 $customerZhongjianSign->save();
              }
          }
        }
        $customer->save();
        return response()->json(
            [
                'status'=>1,
                'message'=>'保存数据成功',
            ]
        );
    }
    /**
     * 获取跟进状态
     * @authenticated
     */
    public function statusV2(){
       $status= Coustomer::$followStatusMap;
       return response()->json(
           [
               'code'=>1,
               'message'=>'请求数据成功',
               'data'=>$status
           ]
       );
    }

}
